﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace RegelungDemo
{
    class Program
    {
        public static int tickCnt = 0;
        public static bool continueRun = true;

        /************************************************/
        public static float ak = 0;
        public static float ak_1 = 0;
        public static float ek = 0;
        public static float ek_1 = 0;


        /***** params *********/
        public static float Ta = 0.01f;     // 10ms Abtastzeit
        public static float T = 1000.0f;    // Zeitkostante
        public static float kp = 1.0f;      // Regler

        // 3.522 => 4,85     // Porsche 911
        // 1.127 =>   15,13   // Toyota Yaris
        // 1.645 =>   10,36   // Audi A6

        public static float k2 = 3.522f;     // Motor


        public static float k1;             // Getriebe
        public static int n = 1;           // Gang

        /************************************************/

        public static void Countdown()
        {
            while (ak < 27.77f)
            {
                k1 = get_k1(ak);

                float P = Ta * kp * k2 * k1 / T;

                ak = ak_1 + P * (ek_1 - ak_1);

                ak_1 = ak;
                ek_1 = ek;

                tickCnt++;
            }
        }

        static void Main(string[] args)
        {
            ek = 69.44f;

            //for (float f = 1.0f; f < 30.0f; f += 0.5f)
            //{
            //    k2 = f;
            //    ak = 0;
            //    ak_1 = 0;
            //    ek_1 = 0;
            //    tickCnt = 0;

                Thread countThread = new Thread(new ThreadStart(Countdown));
                countThread.Start();
                countThread.Join();
                //Thread.Sleep(5);

                Console.WriteLine("Speed: " + Math.Round(ak, 2) + " m/s  Gang: " + n);
                Console.WriteLine("Time: " + (float)tickCnt / 100.0f + "s");
                // Console.WriteLine(k2 + "\t" + (float)tickCnt / 100.0f);
            //}
        }

        public static float get_k1(float speed)
        {
            switch (n)
            {
                case 1:
                    if (speed > 20)
                        n = 2;
                    break;

                case 2:
                    if (speed > 40)
                        n = 3;
                    if (speed < 15)
                        n = 1;
                    break;

                case 3:
                    if (speed > 60)
                        n = 4;
                    if (speed < 35)
                        n = 2;
                    break;

                case 4:
                    if (speed > 85)
                        n = 5;
                    if (speed < 55)
                        n = 3;
                    break;

                case 5:
                    if (speed < 75)
                        n = 4;
                    break;
            }


            switch (n)
            {
                case 1: return 40.0f;
                case 2: return 20.0f;
                case 3: return 13.0f;
                case 4: return 10.0f;
                case 5: return 8.0f;
            }
            return 1.0f;

        }
    }
}
